package com.base.app_core.util;

import android.content.Context;

import com.blankj.utilcode.util.StringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.module.GlideModule;

import java.io.File;

/**
 * Created by jiansheng.li on 2017/5/24.
 */

public class KCGlidModule implements GlideModule {
	@Override
	public void applyOptions(Context context, GlideBuilder builder) {
		builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
		//设置缓存目录
		String filePath="";// AppConstants.Const.CACHE_DIR;
		if(StringUtils.isEmpty(filePath))
			return;
		File cacheDir = new File(filePath);

		final DiskCache cache = DiskLruCacheWrapper.get(cacheDir, DiskCache.Factory.DEFAULT_DISK_CACHE_SIZE);// 250 MB
		builder.setDiskCache(new DiskCache.Factory() {
			@Override
			public DiskCache build() {
				return cache;
			}
		});
		//设置memory和Bitmap池的大小
		MemorySizeCalculator calculator = new MemorySizeCalculator(context);
		int defaultMemoryCacheSize = calculator.getMemoryCacheSize();
		int defaultBitmapPoolSize = calculator.getBitmapPoolSize();

		int customMemoryCacheSize = (int) (1.2 * defaultMemoryCacheSize);
		int customBitmapPoolSize = (int) (1.2 * defaultBitmapPoolSize);

		builder.setMemoryCache(new LruResourceCache(customMemoryCacheSize));
		builder.setBitmapPool(new LruBitmapPool(customBitmapPoolSize));
	}

	@Override
	public void registerComponents(Context context, Glide glide) {

	}
}
